package com.naiterui.ehp.bs.emr.controller.openapi;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.constants.SwaggerTags;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.utils.encypt.DESUtil;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;
import com.naiterui.ehp.bs.emr.service.IOpenEmrService;
import com.naiterui.ehp.bs.emr.vo.openapi.ConsultRecordDetailVO;
import com.naiterui.ehp.bs.emr.vo.openapi.ConsultRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;

/**
 * 病例查询开放接口
 *
 * @author guoyongxiang
 * @version 1.0.0
 * @date 2021/7/6 15:46
 **/
@Slf4j
@Api(tags = SwaggerTag.OPEN_API + SwaggerTag.MEDICAL_RECORD)
@Validated
@RequestMapping("emr/openapi")
@RestController
@RequiredArgsConstructor
public class OpenEmrController {

    private final IOpenEmrService openEmrService;

    /**
     * 获取诊疗记录列表
     *
     * @param key
     * @return
     */
    @ApiOperation(value = "获取诊疗记录列表", notes = "获取诊疗记录列表(对外使用-his嵌入)", tags = SwaggerTags.OPENAPI_EMR)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "身份证号加密串", required = true, dataType = "string", paramType = "query"),
    })
    @GetMapping("cases")
    public ResponseVO<PageVO<ConsultRecordVO>> getCaseList(@NotEmpty(message = "key不能为空") String key, PageParamVO pageParamVO) throws BusinessException {
        log.info("获取诊疗记录列表 key:{}", key);
        String idcard = this.getIdCard(key);
        return PublicService.returnResponseVO(openEmrService.getCaseList(idcard, pageParamVO));
    }

    /**
     * 获取诊疗记录详情
     *
     * @param sessionId
     * @param key
     * @return
     */
    @ApiOperation(value = "获取诊疗记录详情", notes = "获取诊疗记录详情(对外使用-his嵌入)", tags = SwaggerTags.OPENAPI_EMR)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "身份证号加密串", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sessionId", value = "会话ID", required = true, dataType = "string", paramType = "path"),
    })
    @GetMapping("cases/{sessionId}")
    public ResponseVO<ConsultRecordDetailVO> getCaseDetail(@PathVariable String sessionId, @NotEmpty(message = "key不能为空") String key) throws BusinessException {
        log.info("获取诊疗记录详情 key:{}， sessionId:{}", key, sessionId);
        String idcard = this.getIdCard(key);
        return PublicService.returnResponseVO(openEmrService.getCaseDetail(idcard, sessionId));
    }

    /**
     * 获取视频诊疗记录列表
     *
     * @param key
     * @return
     */
    @ApiOperation(value = "获取视频诊疗记录列表", notes = "获取视频诊疗记录列表(对外使用-his嵌入)", tags = SwaggerTags.OPENAPI_EMR)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "key", value = "身份证号加密串", required = true, dataType = "string", paramType = "query"),})
    @GetMapping("cases/video")
    public ResponseVO<PageVO<ConsultRecordVO>> getVideoCaseList(@NotEmpty(message = "key不能为空") String key,
        PageParamVO pageParamVO) throws BusinessException {
        log.info("获取视频诊疗记录列表 key:{}", key);
        String idcard = this.getIdCard(key);
        return PublicService.returnResponseVO(openEmrService.getVideoCaseList(idcard, pageParamVO));
    }

    /**
     * 获取诊疗记录详情
     *
     * @param videoConsultId
     * @param key
     * @return
     */
    @ApiOperation(value = "获取视频诊疗记录详情", notes = "获取视频诊疗记录详情(对外使用-his嵌入)", tags = SwaggerTags.OPENAPI_EMR)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "key", value = "身份证号加密串", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "videoConsultId", value = "视频问诊ID", required = true, dataType = "long",
            paramType = "path"),})
    @GetMapping("cases/video/{videoConsultId}")
    public ResponseVO<ConsultRecordDetailVO> getVideoCaseDetail(@PathVariable Long videoConsultId,
        @NotEmpty(message = "key不能为空") String key) throws BusinessException {
        log.info("获取视频诊疗记录详情 key:{}， videoConsultId:{}", key, videoConsultId);
        String idcard = this.getIdCard(key);
        return PublicService.returnResponseVO(openEmrService.getVideoCaseDetail(idcard, videoConsultId));
    }

    /**
     * 解析患者身份证号
     *
     * @param key
     * @return
     * @throws BusinessException
     */
    public String getIdCard(String key) throws BusinessException {
        String secret = ConfigUtil.getString("his.hisSecret");
        try {
            String paramStr = DESUtil.desDecrypt(secret, key);
            String[] params = paramStr.split("\\|");
            return params[0];
        } catch (Exception e) {
            log.error("开放接口-解密失败 key:{}", key, e);
            throw new BusinessException(ExceptionCodes.SIGN_ERR);
        }
    }
}
